<template>
  <div class="main">
    <breadcrumb/>
    <el-card>
      <el-row>
        <el-col>
          <el-input placeholder="请输入搜索关键字" v-model="queryInfo.query" clearable @clear="getOrders">
            <el-button slot="append" icon="el-icon-search" @click="getOrders"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table stripe border :data="orderList">
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price"></el-table-column>
        <el-table-column label="订单状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pay_status === 1" type="success">已付款</el-tag>
            <el-tag v-else type="danger">未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货">
          <template slot-scope="scope">
            {{scope.row.is_send === 1 ? '是' : '否'}}
          </template>
        </el-table-column>
        <el-table-column label="下单时间">
          <template slot-scope="scope">
            {{scope.row.create_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="">
            <el-button type="primary" size="mini" class="el-icon-edit" @click="showBox"></el-button>
            <el-button type="success" size="mini" class="el-icon-location" @click="showProgressBox"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="(pageSize) => {
          queryInfo.pagesize = pageSize
          getOrders()
        }"
        @current-change="(pageNum) => {
          queryInfo.pagenum = pageNum
          getOrders()
        }"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!--修改地址的对话框-->
    <el-dialog
      title="修改地址"
      :visible.sync="modifyAddressDialogVisible"
      width="50%"
      @close="modifyAddressDialogClosed"
    >
      <el-form :model="modifyForm" ref="modifyFormRef" :rules="modifyFormRules" label-width="100px">
        <el-form-item label="省市区/县：" prop="address1">
          <el-cascader :options="citydata" v-model="modifyForm.address1" expand-trigger="hover"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址：" prop="address2">
          <el-input v-model="modifyForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="modifyAddressDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="modifyAddressDialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>

    <!--物流进度对话框-->
    <el-dialog
      title="物流进度"
      :visible.sync="progressDialogVisible"
      width="50%"
    >
      <el-timeline reverse>
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
    <el-button @click="progressDialogVisible = false">关闭</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import Breadcrumb from '@/components/Breadcrumb'
import { getOrders } from '../../api/orders/order'
import citydata from './citydata'
import { progressData } from './progress'

export default {
  name: 'Orders',
  components: { Breadcrumb },
  data: () => {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      orderList: [],
      total: 0,
      modifyAddressDialogVisible: false,
      progressDialogVisible: false,
      modifyForm: {
        address1: [],
        address2: ''
      },
      modifyFormRules: {
        address1: [{ required: true, message: '请选择省市区/县', trigger: 'blur' }],
        address2: [{ required: true, message: '请填写详细地址', trigger: 'blur' }]
      },
      citydata,
      progressInfo: []
    }
  },
  created () {
    this.getOrders()
  },

  methods: {
    // 关闭编辑弹窗
    modifyAddressDialogClosed () {
      this.$refs.modifyFormRef.resetFields()
      this.modifyAddressDialogVisible = false
    },

    // 点击编辑
    showBox () {
      this.modifyAddressDialogVisible = true
    },

    async showProgressBox () {
      this.progressInfo = progressData.data
      // console.log(this.progressInfo)
      this.progressDialogVisible = true
    },

    // 获取订单列表
    async getOrders () {
      const { data: res } = await getOrders(this.queryInfo)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.orderList = res.data.goods
      // console.log(this.orderList)
      this.total = res.data.total
    }
  }
}
</script>
<style lang="less" scoped>
  @import "./index";
</style>
